package com.zxcz.service.impl;

import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zxcz.aliyun.OssConfig;
import com.zxcz.aliyun.OssManager;
import com.zxcz.domain.Resource;
import com.zxcz.domain.Result;
import com.zxcz.service.OssService;
import com.zxcz.tools.CustomHttpStatus;
import com.zxcz.tools.ResultUtil;

@Service
public class OssServiceImpl implements OssService{
	@Autowired
	private OssConfig ossConfig;
	@Autowired
	private OssManager ossManager;
	private static final Log logger = LogFactory.getLog(OssServiceImpl.class);

	/**
     * 获取上传url
     */
	@Override
	public Result<Object> policy(String filename) {
		// 存储目录
		String objectName = ossConfig.getDir()+filename;
		//获取上传url
		String url = ossManager.getUploadUrl(objectName);
		
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("objectName", objectName);
		map.put("url", url);
		
		logger.info("map --> " + map);
		
		return ResultUtil.generate(CustomHttpStatus.SUCCESS, map);
	}

	@Override
	public Result<Object> deleteResource(Resource resource) {
		// oss对象
		String ossObject = resource.getOssObject();
		if(ossObject == null || ossObject == "") {
			logger.warn("要删除的资源不存在");
			return ResultUtil.generate(CustomHttpStatus.RESOURCE_NOT_FOUND);
		}
		
		// 删除oss上的资源
		ossManager.deleteObject(ossObject);
		
		return ResultUtil.generate(CustomHttpStatus.SUCCESS);
	}

}
